/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int res = 0;
    void dfs(TreeNode* root, int sum)
    {
        if(!root)
            return;
        if(sum - root->val == 0)
            res += 1;
        dfs(root->left, sum-root->val);
        dfs(root->right, sum-root->val);
    }
    int pathSum(TreeNode* root, int sum) {
        if(!root)
            return res;
        pathSum(root->left, sum);
        pathSum(root->right, sum);

        dfs(root, sum);

        return res;
    }
};